Skip to contentMethod: serializeKey(Object, JsonGenerator, SerializationContextImpl)
1: /*
2: * Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0 which is available at
6: * http://www.eclipse.org/legal/epl-2.0,
7: * or the Eclipse Distribution License v. 1.0 which is available at
8: * http://www.eclipse.org/org/documents/edl-v10.php.
9: *
10: * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11: */
12:
13: package org.eclipse.yasson.internal.serializer.types;
14:
15: import jakarta.json.stream.JsonGenerator;
16:
17: import org.eclipse.yasson.internal.SerializationContextImpl;
18: import org.eclipse.yasson.internal.serializer.ModelSerializer;
19:
20: /**
21: * Base for all the type serializers.
22: */
23: abstract class TypeSerializer<T> implements ModelSerializer {
24:
25: private final ModelSerializer serializer;
26:
27: TypeSerializer(TypeSerializerBuilder serializerBuilder) {
28: if (serializerBuilder.isKey()) {
29: serializer = new KeySerializer();
30: } else {
31: serializer = new ValueSerializer();
32: }
33: }
34:
35: @Override
36: public void serialize(Object value, JsonGenerator generator, SerializationContextImpl context) {
37: serializer.serialize(value, generator, context);
38: }
39:
40: abstract void serializeValue(T value, JsonGenerator generator, SerializationContextImpl context);
41:
42: void serializeKey(T key, JsonGenerator generator, SerializationContextImpl context) {
43: generator.writeKey(String.valueOf(key));
44: }
45:
46: private final class ValueSerializer implements ModelSerializer {
47:
48: @SuppressWarnings("unchecked")
49: @Override
50: public void serialize(Object value, JsonGenerator generator, SerializationContextImpl context) {
51: serializeValue((T) value, generator, context);
52: }
53:
54: }
55:
56: private final class KeySerializer implements ModelSerializer {
57:
58: @SuppressWarnings("unchecked")
59: @Override
60: public void serialize(Object value, JsonGenerator generator, SerializationContextImpl context) {
61: serializeKey((T) value, generator, context);
62: }
63:
64: }
65: }